package com.project.service.impl;

import com.project.bean.EmployeeBean;
import com.project.mapper.IEmployeeMapper;
import com.project.service.IEmployeeService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EmployeeServiceImpl extends BaseService implements IEmployeeService {

    @Override
    public void add(EmployeeBean emBean) {
        SqlSession session = this.getSession();
        IEmployeeMapper mapper = session.getMapper(IEmployeeMapper.class);
        mapper.add(emBean);

        session.commit();
        session.close();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
        IEmployeeMapper mapper = session.getMapper(IEmployeeMapper.class);
        mapper.del(id);
        session.commit();
        session.close();
    }

    @Override
    public void update(int id, LocalDate birthday) {
        SqlSession session = this.getSession();
        IEmployeeMapper mapper = session.getMapper(IEmployeeMapper.class);
        mapper.update(id,birthday);

        session.commit();
        session.close();
    }

    @Override
    public List<EmployeeBean> findAll() {
        SqlSession session = this.getSession();
        IEmployeeMapper mapper = session.getMapper(IEmployeeMapper.class);
        List<EmployeeBean> list = mapper.findAll();
        session.close();
        return list;
    }

    @Override
    public List<EmployeeBean> findByItem(String name, String startTime, String endTime) {
        Map map = new HashMap();
        map.put("name",name);
        map.put("startBirth",startTime);
        map.put("endBirth",endTime);
        SqlSession session = this.getSession();
        IEmployeeMapper mapper = session.getMapper(IEmployeeMapper.class);

        List<EmployeeBean> list = mapper.findAll();
        session.close();
        return list;
    }

    @Override
    public EmployeeBean findById(int id) {
        SqlSession session = this.getSession();
        IEmployeeMapper mapper = session.getMapper(IEmployeeMapper.class);
        EmployeeBean em = mapper.findById(id);
        session.close();

        session = this.getSession();
        mapper = session.getMapper(IEmployeeMapper.class);
        em = mapper.findById(id);
        session.close();
        return em;
    }
}
